package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceCode;
import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.BrandCategoryMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.BrandCategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.BrandCategory;
import cn.tedu.csmall.product.pojo.vo.BrandCategoryStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.IBrandCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BrandCategoryServiceImpl implements IBrandCategoryService {
    @Autowired
    private BrandCategoryMapper brandCategoryMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void bind(BrandCategoryAddNewDTO brandCategoryAddNewDTO) {
        log.debug("即将处理插入品牌类别关联数据,参数:{}",brandCategoryAddNewDTO);
        //获取brandId
        //根据brandId 调用brandMapper中的查询详情 方法 看品牌是否存在
        //如果不存在 则抛出异常
        Long brandId = brandCategoryAddNewDTO.getBrandId();
        log.debug("品牌id={}",brandId);
        BrandStandardVO brandMapperStandardById = brandMapper.getStandardById(brandId);
        if (brandMapperStandardById == null) {
            String message = "插入品牌类别关联数据失败,该品牌不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        //获取类别id
        //根据类别id 调用categoryMapper中的查询详情方法 看类别是否存在
        //如果不存在 则抛出异常
        Long categoryId = brandCategoryAddNewDTO.getCategoryId();
        log.debug("类别id={}",categoryId);
        CategoryStandardVO categoryMapperStandardById = categoryMapper.getStandardById(categoryId);
        if (categoryMapperStandardById==null){
            String message = "插入品牌类别关联数据失败,该类别不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //根据 品牌id 与 类别id 调用brandCategoryMapper 中的统计方法
        //如果数量大于零 则 代表绑定关系存在 抛出异常
        int count = brandCategoryMapper.countByBrandIdAndCategoryId(brandId, categoryId);
        if (count>0){
            String message = "插入品牌类别关联数据失败,该关联数据已经存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        //创建brandCategory对象
        //将DTO赋值给他
        //调用插入方法 判断生效的行数
        //若生效的行数不等于1 则抛出异常
        BrandCategory brandCategory = new BrandCategory();
        BeanUtils.copyProperties(brandCategoryAddNewDTO,brandCategory);
        log.debug("即将插入的品牌类别管理数据={}",brandCategory);
        int rows = brandCategoryMapper.insert(brandCategory);
        if (rows!=1){
            String message = "插入品牌类别关联数据失败,服务器忙,请稍后重试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void unbind(Long id) {
        //调用mapper中的根据id查询详情方法
        //判断 数据是否存在 不存在 则抛出异常
        BrandCategoryStandardVO standardById = brandCategoryMapper.getStandardById(id);
        log.debug("查询到的详情:{}",standardById);
        if (standardById==null){
            String message = "删除品牌与类别关联数据失败,该数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        //调用mapper中的根据id删除数据
        //判断生效的行数是否不等于1
        //是 抛出异常
        int rows = brandCategoryMapper.deleteById(id);
        if (rows!=1){
            String message = "解绑(删除)品牌与类别关联数据失败,服务器忙,请稍后重试!";
            log.warn(message);
            throw  new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }
}
